package com.lanhao.aot.util;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

public class AddressParser {
	private static final Set<String> PROVINCES = new HashSet<>(
			Arrays.asList("北京市", "天津市", "上海市", "重庆市", "河北省", "山西省", "辽宁省", "吉林省", "黑龙江省", "江苏省", "浙江省", "安徽省", "福建省",
					"江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "海南省", "四川省", "贵州省", "云南省", "陕西省", "甘肃省", "青海省", "台湾省",
					"内蒙古自治区", "广西壮族自治区", "西藏自治区", "宁夏回族自治区", "新疆维吾尔自治区", "香港特别行政区", "澳门特别行政区"));
	
	// 省>市>区县>乡镇街道>路/巷/街>门牌号码>小区/行政村/单位/楼宇>楼栋>单元>层>房号
	private static final LinkedHashMap<String, Pattern> HIERARCHY = new LinkedHashMap<String, Pattern>() {
		private static final long serialVersionUID = 5049319318938983327L;

	{
        put("province", createProvincePattern());
        put("city", Pattern.compile("^([^市]+?市)"));
        put("district", Pattern.compile("^([^区]+?[区县])"));
        put("township", Pattern.compile("^(.*?(?:街道|镇|乡))"));
        put("road", Pattern.compile("^(.*?(?:路|街|巷|道|大街))"));
        put("village", Pattern.compile("^(.*?(?:村|社区|工业区|开发区|片区))"));
        put("number", Pattern.compile("^(\\D*\\d+-?\\d*号)"));
        put("building", Pattern.compile("^(.*?(?:栋|座|楼|幢|号楼|梯|园区|苑|广场|大厦|中心))"));
        put("unit", Pattern.compile("^(\\d+单元|\\D*单元)"));
        put("floor", Pattern.compile("^(.*?(?:层|楼|F))"));
        put("room", Pattern.compile("^(\\d+室?)$"));
    }};
    
    
    private static Pattern createProvincePattern() {
        String regex = "^(" + String.join("|", PROVINCES) + ")";
        return Pattern.compile(regex);
    }

    public static Map<String, String> parse(String address) {
        // 预处理：移除所有标记信息
        String cleaned = address.replaceAll("[\\$\\【].*?[\\$\\】]", "")
                .replaceAll("\\$.*?\\$", "")
                .replaceAll("【.*?】", "")
                .replaceAll("[号楼](?=\\d)", "号 ") // 分割粘连字符
                .replaceAll("\\s+", " ")
                .trim();
        
        Map<String, String> result = new LinkedHashMap<>();
        String remaining = cleaned;

        for (Map.Entry<String, Pattern> entry : HIERARCHY.entrySet()) {
            Matcher matcher = entry.getValue().matcher(remaining);
            if (matcher.find()) {
                String value = matcher.group(1).trim();
                result.put(entry.getKey(), value);
                remaining = remaining.substring(matcher.end()).trim();
            } else {
                result.put(entry.getKey(), "");
            }
        }

        return result;
    }

    public static void main(String[] args) {
//        List<String> testCases = Arrays.asList(
//            "广州市白云区同和街道白山东路同和村白山片区白山西街105号1层103",
//            "广州市白云区江高镇广花四路双岗村安二街18号2层201",
//            "广州市白云区均禾街道广花一路石马村桃源南中街四巷2号1层104",
//            "广州市白云区太和镇S116省道太和白山村泰山李一路自编17-1号1层110",
//            "广州市增城区新塘镇汇太东路汇美区岭南路8号4层406",
//            "合肥市高新区望江西路666号科大讯飞园区A5楼21层",
//            "安徽省合肥市蜀山区百草街海亮九玺北3栋1704"
//        );
        
        try {
			List<String> testCases = FileUtils.readLines(new File("E:/222222.txt"), "UTF-8");
			for (String addr : testCases) {
				Map<String, String> parsed = parse(addr);
				System.out.println("解析地址: " + addr);
				parsed.forEach((k, v) -> System.out.printf("%-10s: %s%n", k, v));
				System.out.println("----------------------");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

    }
}
